package com.grab.page.controller;

import com.grab.page.utils.CrawlerUtils;
import com.grab.page.utils.MessyCodeCheckUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.MalformedURLException;
import java.net.URL;

@RestController
@RequestMapping("/hello")
public class HellWord {

    @RequestMapping("/")
    public String helloWord(String url) throws Exception {

        String charset = CrawlerUtils.getCharset(null, null, url,"GET");
        String html =  CrawlerUtils.getContent(null, null, url,"GET",charset);
        Document document = Jsoup.parse(html);

        URL  gethost_url = new URL(url);
        String host = gethost_url.getHost();

        String result = "";
        if(StringUtils.equals(host,"zhuanlan.zhihu.com")){
            Elements article = document.getElementsByTag("article");
            result = article.text();
        }else if(StringUtils.equals(host,"juejin.cn")){
            Elements article = document.getElementsByAttributeValue("itemprop","articleBody");
            result = article.text();
        }else{
            result = document.text();
        }
        return result;
    }
}
